package top.infopub.mgr.bpa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.dto.bpa.PrimarySureDto;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.bpa.dao.DeviceSureAttDao;
import top.infopub.mgr.bpa.dao.DeviceSureDao;
import top.infopub.mgr.bpa.dao.DeviceSureDetailDao;
import top.infopub.mgr.bpa.domain.DeviceSureDomain;
import top.infopub.mgr.bpa.service.DeviceSureService;
import top.infopub.mgr.bpa.vo.DeviceSureVo;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.util.ReflectBeanUtils;
import top.infopub.model.bpa.DeviceSure;
import top.infopub.model.bpa.DeviceSureAtt;
import top.infopub.model.bpa.DeviceSureDetail;

import java.util.Date;
import java.util.List;

@Service
public class DeviceSureServiceImpl extends GenericServiceImpl<DeviceSure, String> implements
		DeviceSureService {

	@Autowired
	private DeviceSureDao deviceSureDao;
	
	@Autowired
    private WsFlowProcessService wsFlowProcess;

    @Autowired
    private DeviceSureDetailDao deviceSureDetailDao;
    
    @Autowired
    private DeviceSureAttDao deviceSureAttDao;

	@Override
	public GenericDao<DeviceSure, String> getDao() {
		return deviceSureDao;
	}

    @Override
    public Page<DeviceSureDomain> selectPageByDomainWhere(Page<DeviceSureDomain> page,
                                                          DeviceSureVo deviceSureVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<DeviceSureDomain>)deviceSureDao.selectByDomainWhere(deviceSureVo);
    }

    @Override
    public Page<DeviceSureDomain> selectMyCheckByDomainWhere(Page<DeviceSureDomain> page,
                                                             DeviceSureVo deviceSureVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<DeviceSureDomain>)deviceSureDao.selectMyCheckByDomainWhere(deviceSureVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(DeviceSureVo deviceSure, List<DeviceSureDetail> deviceSureDetails,
                     List<DeviceSureAtt> deviceSureAtts) {
        //插入记录
        deviceSureDao.insertById(deviceSure);

        //批量插入子项
        if(CollectionUtils.isNotEmpty(deviceSureDetails)){
            deviceSureDetailDao.insertByBatch(deviceSureDetails);
        }
        
        //批量插入附件
        if(CollectionUtils.isNotEmpty(deviceSureAtts)){
            deviceSureAttDao.insertByBatch(deviceSureAtts);
        }
        
        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.BPA_DEVICE_SURE;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, deviceSure.getId(), deviceSure.getProcessId(), null);
        
    }

    @Override
    public DeviceSureVo queryById(String id) {
        return deviceSureDao.queryById(id);
    }

    @Override
    public String insertById(PrimarySureDto primarySureDto, String creator) {
        DeviceSureVo deviceSureVo = ReflectBeanUtils.do2bo(primarySureDto, DeviceSureVo.class);
        Date date = new Date();
        deviceSureVo.setAddBy(creator);
        deviceSureVo.setAddTime(date);
        deviceSureVo.setUptBy(creator);
        deviceSureVo.setUptTime(date);
        deviceSureDao.insertSelective(deviceSureVo);
        return deviceSureVo.getId();
    }

    @Override
    public void updateByIdSelective(PrimarySureDto primarySureDto, String modifier) {
        DeviceSureVo deviceSureVo = ReflectBeanUtils.do2bo(primarySureDto, DeviceSureVo.class);
        deviceSureVo.setId(primarySureDto.getId());
        Date date = new Date();
        deviceSureVo.setUptBy(modifier);
        deviceSureVo.setUptTime(date);
        deviceSureDao.updateByIdSelective(deviceSureVo);
    }

}